package com.net.oil.order.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.order.bean.MaintainDetailBean;
import com.net.oil.order.contract.MaintainDetailContract;
import com.net.oil.order.presenter.MaintainDetailPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/6/7 0007:下午 22:06
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class MaintainDetailModel extends BaseModel implements MaintainDetailContract.Model {

    private MaintainDetailPresenter presenter;

    public MaintainDetailModel(MaintainDetailPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void getMaintainDetail(int id) {
        AppHttpClient.ApiService().getMaintainDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MaintainDetailBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<MaintainDetailBean> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null) {
                            presenter.getMaintainDetailSuccess(tResultBean.getData());
                        } else {
                            presenter.getMaintainDetailFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getMaintainDetailFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void commitComment(int id, int score, String commentDesc) {
        AppHttpClient.ApiService().commitComment(id, score, commentDesc)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.commitCommentSuccess();
                        }else {
                            presenter.commitCommentFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.commitCommentFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
